package cn.iocoder.yudao.module.design.service.probleminnerrel;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.probleminnerrel.vo.ProblemInnerRelCreateReqVO;
import cn.iocoder.yudao.module.design.controller.admin.probleminnerrel.vo.ProblemInnerRelExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.probleminnerrel.vo.ProblemInnerRelPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.probleminnerrel.vo.ProblemInnerRelUpdateReqVO;
import cn.iocoder.yudao.module.design.convert.probleminnerrel.ProblemInnerRelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.probleminnerrel.ProblemInnerRelDO;
import cn.iocoder.yudao.module.design.dal.mysql.probleminnerrel.ProblemInnerRelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.PROBLEM_INNER_REL_NOT_EXISTS;

/**
 * 问题间关联 Service 实现类
 *
 * @author czw
 */
@Service
@Validated
public class ProblemInnerRelServiceImpl implements ProblemInnerRelService {

    @Resource
    private ProblemInnerRelMapper problemInnerRelMapper;

    @Override
    public Long createProblemInnerRel(ProblemInnerRelCreateReqVO createReqVO) {
        // 插入
        ProblemInnerRelDO problemInnerRel = ProblemInnerRelConvert.INSTANCE.convert(createReqVO);
        problemInnerRel.setDeleted(false);
        problemInnerRelMapper.insert(problemInnerRel);
        // 返回
        return problemInnerRel.getId();
    }

    @Override
    public void updateProblemInnerRel(ProblemInnerRelUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProblemInnerRelExists(updateReqVO.getId());
        // 更新
        ProblemInnerRelDO updateObj = ProblemInnerRelConvert.INSTANCE.convert(updateReqVO);
        problemInnerRelMapper.updateById(updateObj);
    }

    @Override
    public void deleteProblemInnerRel(Long id) {
        // 校验存在
        this.validateProblemInnerRelExists(id);
        // 删除
        problemInnerRelMapper.deleteById(id);
    }

    private void validateProblemInnerRelExists(Long id) {
        if (problemInnerRelMapper.selectById(id) == null) {
            throw exception(PROBLEM_INNER_REL_NOT_EXISTS);
        }
    }

    @Override
    public ProblemInnerRelDO getProblemInnerRel(Long id) {
        return problemInnerRelMapper.selectById(id);
    }

    @Override
    public List<ProblemInnerRelDO> getProblemInnerRelList(Collection<Long> ids) {
        return problemInnerRelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProblemInnerRelDO> getProblemInnerRelPage(ProblemInnerRelPageReqVO pageReqVO) {
        return problemInnerRelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProblemInnerRelDO> getProblemInnerRelList(ProblemInnerRelExportReqVO exportReqVO) {
        return problemInnerRelMapper.selectList(exportReqVO);
    }

}
